package com.sanqing.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sanqing.dao.base.BaseDao;
import com.sanqing.po.Subject;
import com.sanqing.service.SubjectService;
import com.sanqing.util.Page;
import com.sanqing.util.PageResult;
import com.sanqing.util.PageUtil;
import com.sun.xml.internal.bind.v2.model.core.ID;

@Service("subjectService")
@Transactional(readOnly = true)
public class SubjectServiceImpl implements SubjectService {

	@Resource(name = "subjectDao")
	private BaseDao<Subject, String> subjectDao;

	private POIFSFileSystem fs;
	private HSSFWorkbook wb;
	private HSSFSheet sheet;
	private HSSFRow row;

	@Override
	public PageResult querySubjectByPage(Page page, Subject subject) {
		Map<String, Object> map = new HashMap<String, Object>();
		String hql = "from Subject a where 1=1";

		if (StringUtils.isNotBlank(subject.getSubjectTitle())) {
			hql += " and a.subjectTitle like:subjectTitle";
			map.put("subjectTitle", "%" + subject.getSubjectTitle() + "%");
		}
		page = PageUtil.createPage(page.getEveryPage(), (int) subjectDao.countHqlResult(hql, map), page.getCurrentPage());// 根据总记录数创建分页信息
		List<Subject> list = subjectDao.findByPage(page, hql, map);// 通过分页信息取得试题
		PageResult result = new PageResult(page, list);// 封装分页信息和记录信息，返回给调用处
		return result;
	}

	@Override
	@Transactional
	public void add(Subject subject) {
		subjectDao.save(subject);
	}

	@Override
	public Subject getById(String id) {
		return subjectDao.getByKey(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void edit(Subject subject) {
		subjectDao.saveOrUpdate(subject);
	}

	@Override
	@Transactional
	public void delete(String id) {
		subjectDao.delete(id);
	}

	@Override
	@Transactional(readOnly = false)
	public int addBatch(File myFile, Subject subject) throws Exception {
		FileInputStream fis = new FileInputStream(myFile);
		fs = new POIFSFileSystem(fis);
		wb = new HSSFWorkbook(fs);
		sheet = wb.getSheetAt(0);
		// 得到总行数
		int rowNum = sheet.getLastRowNum();
		row = sheet.getRow(0);
		for (int i = 1; i <= rowNum; i++) {
			Subject s = new Subject();
			s.setSubjectType(subject.getSubjectType());
			s.setCourse(subject.getCourse());

			row = sheet.getRow(i);
			if (row.getCell(0) != null) {
				if (row.getCell(1) != null)
					row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
				if (row.getCell(2) != null)
					row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
				if (row.getCell(3) != null)
					row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
				if (row.getCell(4) != null)
					row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
				if (row.getCell(5) != null)
					row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
				if (row.getCell(6) != null)
					row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
				if (row.getCell(7) != null)
					row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
				s.setSubjectTitle(row.getCell(1).getStringCellValue());
				s.setSubjectOptionA(row.getCell(2).getStringCellValue());
				s.setSubjectOptionB(row.getCell(3).getStringCellValue());
				s.setSubjectOptionC(row.getCell(4).getStringCellValue());
				s.setSubjectOptionD(row.getCell(5).getStringCellValue());
				s.setSubjectAnswer(row.getCell(6).getStringCellValue());
				s.setSubjectParse(row.getCell(7).getStringCellValue());
				subjectDao.save(s);
			}
		}

		return rowNum;
	}

}
